package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ILawCaseService;
import com.mdd.admin.validate.LawCaseCreateValidate;
import com.mdd.admin.validate.LawCaseUpdateValidate;
import com.mdd.admin.validate.LawCaseSearchValidate;
import com.mdd.admin.vo.LawCaseListedVo;
import com.mdd.admin.vo.LawCaseDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LawCase;
import com.mdd.common.mapper.LawCaseMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 案件基本信息实现类
 * @author LikeAdmin
 */
@Service
public class LawCaseServiceImpl implements ILawCaseService {
        
    @Resource
    LawCaseMapper lawCaseMapper;

    /**
     * 案件基本信息列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LawCaseListedVo>
     */
    @Override
    public PageResult<LawCaseListedVo> list(PageValidate pageValidate, LawCaseSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<LawCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        lawCaseMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:proposalName@proposal_name:str",
            "like:englishProposalName@english_proposal_name:str",
            "like:otherProposalName@other_proposal_name:str",
            "=:patentLabel@patent_label:str",
            "=:ourRef@our_ref:str",
            "=:businessType@business_type:str",
            "=:country:str",
            "=:customerDocumentNumber@customer_document_number:str",
            "=:caseFlow@case_flow:str",
            "=:countryOrigin@country_origin:str",
            "=:agency:str",
            "=:proposalForm@proposal_form:str",
            "=:initialPeriod@initial_period:str",
            "=:contractNo@contract_no:int",
            "=:applyAnnualFeestage@apply_annual_feestage:str",
            "=:periodGrace@period_grace:str",
            "=:designatedContractingParty@designated_contracting_party:str",
            "=:agent:str",
            "=:agencyCaseNumber@agency_case_number:str",
            "=:isWhetherPaper@is_whether_paper:str",
            "=:technosphere:str",
            "=:newApplicationDate@new_application_date:str",
            "=:flowPersonnel@flow_personnel:str",
            "=:authorizedIssuing@authorized_issuing:str",
            "=:rest:str",
            "=:applicationNumber@application_number:str",
            "=:applicationDate@application_date:str",
            "=:publicationNumber@publication_number:str",
            "=:publicationDate@publication_date:str",
            "=:originalApplicationNumber@original_application_number:str",
            "=:originalApplicationDate@original_application_date:str",
            "=:originalReleaseNumber@original_release_number:str",
            "=:originalReleaseDate@original_release_date:str",
            "=:originalAnnouncementNumber@original_announcement_number:str",
            "=:originalAnnouncementDate@original_announcement_date:str",
            "=:pctApplicationNumber@pct_application_number:str",
            "=:pctApplicationDate@pct_application_date:str",
            "=:pctCity@pct_city:str",
            "=:pctOriginalReleaseDate@pct_original_release_date:str",
            "=:pctOriginalReleaseNumber@pct_original_release_number:str",
            "=:pctPublicationLanguage@pct_publication_language:str",
            "=:deferredExamination@deferred_examination:str",
            "=:isFirstApplication@is_first_application:str",
            "=:caseCoefficient@case_coefficient:str",
            "=:productCategory@product_category:str",
            "=:cost:str",
            "=:budget:str",
            "=:annualMonitoringFee@annual_monitoring_fee:str",
            "=:contactingUnit@contacting_unit:str",
            "=:protectionCenter@protection_center:str",
            "=:contractLife@contract_life:str",
            "=:orginalPtcApplicationNumber@orginal_ptc_application_number:str",
            "=:notes:str",
            "=:trademarkClass@trademark_class:str",
            "=:trademarkType@trademark_type:str",
            "=:buinessPersionnelServicer@buiness_persionnel_servicer:str",
            "=:buinessAssistant@buiness_assistant:str",
            "=:colorMode@color_mode:str",
            "=:isLeadCese@is_lead_cese:str",
            "=:registrationNumber@registration_number:str",
            "=:appointedColor@appointed_color:str",
            "=:isWhetherFlag@is_whether_flag:str",
            "=:prelimianaryDate@prelimianary_date:str",
            "=:prelimianaryDay@prelimianary_day:str",
            "=:approvalDate@approval_date:str",
            "=:extensionDate@extension_date:str",
            "=:terminationDate@termination_date:str",
            "=:trademarkDescriptions@trademark_descriptions:str",
            "=:copyrightType@copyright_type:str",
            "=:isOrgaincInorganic@is_orgainc_inorganic:str",
            "=:isSubsidizeNot@is_subsidize_not:str",
            "=:referenceNumber@reference_number:str",
            "=:author:str",
            "=:homeLocation@home_location:str",
            "=:softwareAbbreviation@software_abbreviation:str",
            "=:versionNumber@version_number:str",
            "=:softwareWorkDescription@software_work_description:str",
            "=:hardwareEnvironment@hardware_environment:str",
            "=:sofwareEnvironment@sofware_environment:str",
            "=:programmingLanguage@programming_language:str",
            "=:sourceQuantity@source_quantity:str",
            "=:mainFunctions@main_functions:str",
            "=:publishedStatus@published_status:str",
            "=:developmentCompletionDate@development_completion_date:str",
            "=:undertakingId@undertaking_id:int",
            "=:feeId@fee_id:int",
            "=:priorityInformationId@priority_information_id:int",
            "=:supportLitigationActionsId@support_litigation_actions_id:int",
            "=:caseRequirementId@case_requirement_id:int",
            "=:countId@count_id:int",
            "=:commdoity:int",
            "=:extendInformationId@extend_information_id:int",
            "=:domesticBasisId@domestic_basis_id:int",
            "=:trademarkFormId@trademark_form_id:str",
            "=:userId@user_id:int",
            "like:userName@user_name:str",
            "=:updateId@update_id:int",
            "like:updateName@update_name:str",
        });

        IPage<LawCase> iPage = lawCaseMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<LawCaseListedVo> list = new LinkedList<>();
        for(LawCase item : iPage.getRecords()) {
            LawCaseListedVo vo = new LawCaseListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 案件基本信息详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LawCase
     */
    @Override
    public LawCaseDetailVo detail(Integer id) {
        LawCase model = lawCaseMapper.selectOne(
                new QueryWrapper<LawCase>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LawCaseDetailVo vo = new LawCaseDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 案件基本信息新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(LawCaseCreateValidate createValidate) {
        LawCase model = new LawCase();
        model.setProposalName(createValidate.getProposalName());
        model.setEnglishProposalName(createValidate.getEnglishProposalName());
        model.setOtherProposalName(createValidate.getOtherProposalName());
        model.setPatentLabel(createValidate.getPatentLabel());
        model.setOurRef(createValidate.getOurRef());
        model.setBusinessType(createValidate.getBusinessType());
        model.setCountry(createValidate.getCountry());
        model.setCustomerDocumentNumber(createValidate.getCustomerDocumentNumber());
        model.setCaseFlow(createValidate.getCaseFlow());
        model.setCountryOrigin(createValidate.getCountryOrigin());
        model.setAgency(createValidate.getAgency());
        model.setProposalForm(createValidate.getProposalForm());
        model.setInitialPeriod(createValidate.getInitialPeriod());
        model.setContractNo(createValidate.getContractNo());
        model.setApplyAnnualFeestage(createValidate.getApplyAnnualFeestage());
        model.setPeriodGrace(createValidate.getPeriodGrace());
        model.setDesignatedContractingParty(createValidate.getDesignatedContractingParty());
        model.setAgent(createValidate.getAgent());
        model.setAgencyCaseNumber(createValidate.getAgencyCaseNumber());
        model.setIsWhetherPaper(createValidate.getIsWhetherPaper());
        model.setTechnosphere(createValidate.getTechnosphere());
        model.setNewApplicationDate(createValidate.getNewApplicationDate());
        model.setFlowPersonnel(createValidate.getFlowPersonnel());
        model.setAuthorizedIssuing(createValidate.getAuthorizedIssuing());
        model.setRest(createValidate.getRest());
        model.setApplicationNumber(createValidate.getApplicationNumber());
        model.setApplicationDate(createValidate.getApplicationDate());
        model.setPublicationNumber(createValidate.getPublicationNumber());
        model.setPublicationDate(createValidate.getPublicationDate());
        model.setOriginalApplicationNumber(createValidate.getOriginalApplicationNumber());
        model.setOriginalApplicationDate(createValidate.getOriginalApplicationDate());
        model.setOriginalReleaseNumber(createValidate.getOriginalReleaseNumber());
        model.setOriginalReleaseDate(createValidate.getOriginalReleaseDate());
        model.setOriginalAnnouncementNumber(createValidate.getOriginalAnnouncementNumber());
        model.setOriginalAnnouncementDate(createValidate.getOriginalAnnouncementDate());
        model.setPctApplicationNumber(createValidate.getPctApplicationNumber());
        model.setPctApplicationDate(createValidate.getPctApplicationDate());
        model.setPctCity(createValidate.getPctCity());
        model.setPctOriginalReleaseDate(createValidate.getPctOriginalReleaseDate());
        model.setPctOriginalReleaseNumber(createValidate.getPctOriginalReleaseNumber());
        model.setPctPublicationLanguage(createValidate.getPctPublicationLanguage());
        model.setDeferredExamination(createValidate.getDeferredExamination());
        model.setIsFirstApplication(createValidate.getIsFirstApplication());
        model.setCaseCoefficient(createValidate.getCaseCoefficient());
        model.setProductCategory(createValidate.getProductCategory());
        model.setCost(createValidate.getCost());
        model.setBudget(createValidate.getBudget());
        model.setAnnualMonitoringFee(createValidate.getAnnualMonitoringFee());
        model.setContactingUnit(createValidate.getContactingUnit());
        model.setProtectionCenter(createValidate.getProtectionCenter());
        model.setContractLife(createValidate.getContractLife());
        model.setOrginalPtcApplicationNumber(createValidate.getOrginalPtcApplicationNumber());
        model.setNotes(createValidate.getNotes());
        model.setTrademarkClass(createValidate.getTrademarkClass());
        model.setTrademarkType(createValidate.getTrademarkType());
        model.setBuinessPersionnelServicer(createValidate.getBuinessPersionnelServicer());
        model.setBuinessAssistant(createValidate.getBuinessAssistant());
        model.setColorMode(createValidate.getColorMode());
        model.setIsLeadCese(createValidate.getIsLeadCese());
        model.setRegistrationNumber(createValidate.getRegistrationNumber());
        model.setAppointedColor(createValidate.getAppointedColor());
        model.setIsWhetherFlag(createValidate.getIsWhetherFlag());
        model.setPrelimianaryDate(createValidate.getPrelimianaryDate());
        model.setPrelimianaryDay(createValidate.getPrelimianaryDay());
        model.setApprovalDate(createValidate.getApprovalDate());
        model.setExtensionDate(createValidate.getExtensionDate());
        model.setTerminationDate(createValidate.getTerminationDate());
        model.setTrademarkDescriptions(createValidate.getTrademarkDescriptions());
        model.setCopyrightType(createValidate.getCopyrightType());
        model.setIsOrgaincInorganic(createValidate.getIsOrgaincInorganic());
        model.setIsSubsidizeNot(createValidate.getIsSubsidizeNot());
        model.setReferenceNumber(createValidate.getReferenceNumber());
        model.setAuthor(createValidate.getAuthor());
        model.setHomeLocation(createValidate.getHomeLocation());
        model.setSoftwareAbbreviation(createValidate.getSoftwareAbbreviation());
        model.setVersionNumber(createValidate.getVersionNumber());
        model.setSoftwareWorkDescription(createValidate.getSoftwareWorkDescription());
        model.setHardwareEnvironment(createValidate.getHardwareEnvironment());
        model.setSofwareEnvironment(createValidate.getSofwareEnvironment());
        model.setProgrammingLanguage(createValidate.getProgrammingLanguage());
        model.setSourceQuantity(createValidate.getSourceQuantity());
        model.setMainFunctions(createValidate.getMainFunctions());
        model.setPublishedStatus(createValidate.getPublishedStatus());
        model.setDevelopmentCompletionDate(createValidate.getDevelopmentCompletionDate());
        model.setUndertakingId(createValidate.getUndertakingId());
        model.setFeeId(createValidate.getFeeId());
        model.setPriorityInformationId(createValidate.getPriorityInformationId());
        model.setSupportLitigationActionsId(createValidate.getSupportLitigationActionsId());
        model.setCaseRequirementId(createValidate.getCaseRequirementId());
        model.setCountId(createValidate.getCountId());
        model.setCommdoity(createValidate.getCommdoity());
        model.setExtendInformationId(createValidate.getExtendInformationId());
        model.setDomesticBasisId(createValidate.getDomesticBasisId());
        model.setTrademarkFormId(createValidate.getTrademarkFormId());
        model.setUserId(createValidate.getUserId());
        model.setUserName(createValidate.getUserName());
        model.setUpdateId(createValidate.getUpdateId());
        model.setUpdateName(createValidate.getUpdateName());
        lawCaseMapper.insert(model);
    }

    /**
     * 案件基本信息编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LawCaseUpdateValidate updateValidate) {
        LawCase model = lawCaseMapper.selectOne(
                new QueryWrapper<LawCase>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setProposalName(updateValidate.getProposalName());
        model.setEnglishProposalName(updateValidate.getEnglishProposalName());
        model.setOtherProposalName(updateValidate.getOtherProposalName());
        model.setPatentLabel(updateValidate.getPatentLabel());
        model.setOurRef(updateValidate.getOurRef());
        model.setBusinessType(updateValidate.getBusinessType());
        model.setCountry(updateValidate.getCountry());
        model.setCustomerDocumentNumber(updateValidate.getCustomerDocumentNumber());
        model.setCaseFlow(updateValidate.getCaseFlow());
        model.setCountryOrigin(updateValidate.getCountryOrigin());
        model.setAgency(updateValidate.getAgency());
        model.setProposalForm(updateValidate.getProposalForm());
        model.setInitialPeriod(updateValidate.getInitialPeriod());
        model.setContractNo(updateValidate.getContractNo());
        model.setApplyAnnualFeestage(updateValidate.getApplyAnnualFeestage());
        model.setPeriodGrace(updateValidate.getPeriodGrace());
        model.setDesignatedContractingParty(updateValidate.getDesignatedContractingParty());
        model.setAgent(updateValidate.getAgent());
        model.setAgencyCaseNumber(updateValidate.getAgencyCaseNumber());
        model.setIsWhetherPaper(updateValidate.getIsWhetherPaper());
        model.setTechnosphere(updateValidate.getTechnosphere());
        model.setNewApplicationDate(updateValidate.getNewApplicationDate());
        model.setFlowPersonnel(updateValidate.getFlowPersonnel());
        model.setAuthorizedIssuing(updateValidate.getAuthorizedIssuing());
        model.setRest(updateValidate.getRest());
        model.setApplicationNumber(updateValidate.getApplicationNumber());
        model.setApplicationDate(updateValidate.getApplicationDate());
        model.setPublicationNumber(updateValidate.getPublicationNumber());
        model.setPublicationDate(updateValidate.getPublicationDate());
        model.setOriginalApplicationNumber(updateValidate.getOriginalApplicationNumber());
        model.setOriginalApplicationDate(updateValidate.getOriginalApplicationDate());
        model.setOriginalReleaseNumber(updateValidate.getOriginalReleaseNumber());
        model.setOriginalReleaseDate(updateValidate.getOriginalReleaseDate());
        model.setOriginalAnnouncementNumber(updateValidate.getOriginalAnnouncementNumber());
        model.setOriginalAnnouncementDate(updateValidate.getOriginalAnnouncementDate());
        model.setPctApplicationNumber(updateValidate.getPctApplicationNumber());
        model.setPctApplicationDate(updateValidate.getPctApplicationDate());
        model.setPctCity(updateValidate.getPctCity());
        model.setPctOriginalReleaseDate(updateValidate.getPctOriginalReleaseDate());
        model.setPctOriginalReleaseNumber(updateValidate.getPctOriginalReleaseNumber());
        model.setPctPublicationLanguage(updateValidate.getPctPublicationLanguage());
        model.setDeferredExamination(updateValidate.getDeferredExamination());
        model.setIsFirstApplication(updateValidate.getIsFirstApplication());
        model.setCaseCoefficient(updateValidate.getCaseCoefficient());
        model.setProductCategory(updateValidate.getProductCategory());
        model.setCost(updateValidate.getCost());
        model.setBudget(updateValidate.getBudget());
        model.setAnnualMonitoringFee(updateValidate.getAnnualMonitoringFee());
        model.setContactingUnit(updateValidate.getContactingUnit());
        model.setProtectionCenter(updateValidate.getProtectionCenter());
        model.setContractLife(updateValidate.getContractLife());
        model.setOrginalPtcApplicationNumber(updateValidate.getOrginalPtcApplicationNumber());
        model.setNotes(updateValidate.getNotes());
        model.setTrademarkClass(updateValidate.getTrademarkClass());
        model.setTrademarkType(updateValidate.getTrademarkType());
        model.setBuinessPersionnelServicer(updateValidate.getBuinessPersionnelServicer());
        model.setBuinessAssistant(updateValidate.getBuinessAssistant());
        model.setColorMode(updateValidate.getColorMode());
        model.setIsLeadCese(updateValidate.getIsLeadCese());
        model.setRegistrationNumber(updateValidate.getRegistrationNumber());
        model.setAppointedColor(updateValidate.getAppointedColor());
        model.setIsWhetherFlag(updateValidate.getIsWhetherFlag());
        model.setPrelimianaryDate(updateValidate.getPrelimianaryDate());
        model.setPrelimianaryDay(updateValidate.getPrelimianaryDay());
        model.setApprovalDate(updateValidate.getApprovalDate());
        model.setExtensionDate(updateValidate.getExtensionDate());
        model.setTerminationDate(updateValidate.getTerminationDate());
        model.setTrademarkDescriptions(updateValidate.getTrademarkDescriptions());
        model.setCopyrightType(updateValidate.getCopyrightType());
        model.setIsOrgaincInorganic(updateValidate.getIsOrgaincInorganic());
        model.setIsSubsidizeNot(updateValidate.getIsSubsidizeNot());
        model.setReferenceNumber(updateValidate.getReferenceNumber());
        model.setAuthor(updateValidate.getAuthor());
        model.setHomeLocation(updateValidate.getHomeLocation());
        model.setSoftwareAbbreviation(updateValidate.getSoftwareAbbreviation());
        model.setVersionNumber(updateValidate.getVersionNumber());
        model.setSoftwareWorkDescription(updateValidate.getSoftwareWorkDescription());
        model.setHardwareEnvironment(updateValidate.getHardwareEnvironment());
        model.setSofwareEnvironment(updateValidate.getSofwareEnvironment());
        model.setProgrammingLanguage(updateValidate.getProgrammingLanguage());
        model.setSourceQuantity(updateValidate.getSourceQuantity());
        model.setMainFunctions(updateValidate.getMainFunctions());
        model.setPublishedStatus(updateValidate.getPublishedStatus());
        model.setDevelopmentCompletionDate(updateValidate.getDevelopmentCompletionDate());
        model.setUndertakingId(updateValidate.getUndertakingId());
        model.setFeeId(updateValidate.getFeeId());
        model.setPriorityInformationId(updateValidate.getPriorityInformationId());
        model.setSupportLitigationActionsId(updateValidate.getSupportLitigationActionsId());
        model.setCaseRequirementId(updateValidate.getCaseRequirementId());
        model.setCountId(updateValidate.getCountId());
        model.setCommdoity(updateValidate.getCommdoity());
        model.setExtendInformationId(updateValidate.getExtendInformationId());
        model.setDomesticBasisId(updateValidate.getDomesticBasisId());
        model.setTrademarkFormId(updateValidate.getTrademarkFormId());
        model.setUserId(updateValidate.getUserId());
        model.setUserName(updateValidate.getUserName());
        model.setUpdateId(updateValidate.getUpdateId());
        model.setUpdateName(updateValidate.getUpdateName());
        lawCaseMapper.updateById(model);
    }

    /**
     * 案件基本信息删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LawCase model = lawCaseMapper.selectOne(
                new QueryWrapper<LawCase>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        lawCaseMapper.updateById(model);
    }

}
